#include "udpsocketimpl.h"

CUdpSocketImpl::CUdpSocketImpl()
{
    m_sock = INVALID_SOCKET;
}
CUdpSocketImpl::~CUdpSocketImpl()
{
    logi("CUdpSocketImpl::~CUdpSocketImpl");
}
HRESULT CUdpSocketImpl::CreateIoOperation(IAsynFrame* pIAsynFrame)
{
    HRESULT hr = S_OK;

    hr = pIAsynFrame->CreateIo(io_operation, (IBase**)&m_pReadOperation);
    rc_error(hr == S_OK, E_FAIL);

    hr = pIAsynFrame->CreateIo(io_operation, (IBase**)&m_pWriteOperation);
    rc_error(hr == S_OK, E_FAIL);

    return S_OK;
}
HRESULT CUdpSocketImpl::BindEvloop(ioevloop_t* loop)
{
    return S_OK;
}

HRESULT CUdpSocketImpl::CloseIo(UINT rw)
{
    return S_OK;
}
HRESULT CUdpSocketImpl::BindIo()
{
    rc_error(m_sock != INVALID_SOCKET, S_ERROR);
    return S_OK;
}
HRESULT CUdpSocketImpl::CUdpSocketImpl::OpenIo()
{
    return S_OK;
}
HRESULT CUdpSocketImpl::BindEvent(IBase* pSocketEvent)
{
    return S_OK;
}
HRESULT CUdpSocketImpl::UnBindEvent()
{
    return S_OK;
}
HRESULT CUdpSocketImpl::CreateSock()
{
    m_sock = INVALID_SOCKET;
    m_sock = _createsock(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    rc_error(m_sock != INVALID_SOCKET, E_FAIL);
    return S_OK;
}
HRESULT CUdpSocketImpl::CloseSock()
{
    rc_error(m_sock != INVALID_SOCKET, E_FAIL);
    _closesock(m_sock);
    m_sock = INVALID_SOCKET;
    return S_OK;
}
HRESULT CUdpSocketImpl::IsOpen()
{
    return S_OK;
}
HRESULT CUdpSocketImpl::AttachSock(_sock_t sock)
{
    rc_error(sock != INVALID_SOCKET, E_FAIL);
    m_sock = sock;
    return S_OK;
}
_sock_t CUdpSocketImpl::DetachSock()
{
    _sock_t sock;
    sock = m_sock;
    m_sock = INVALID_SOCKET;
    return sock;
}
_sock_t CUdpSocketImpl::GetSock()
{
    return m_sock;
}
HRESULT CUdpSocketImpl::GetReadOperation(IID id, IBase** pBase)
{
    rc_error(m_pReadOperation.m_p != NULL, E_FAIL);
    return this->m_pReadOperation->QueryInterface(id, (void**)pBase);
}
HRESULT CUdpSocketImpl::GetWriteOperation(IID id, IBase** pBase)
{
    rc_error(m_pWriteOperation.m_p != NULL, E_FAIL);
    return this->m_pWriteOperation->QueryInterface(id, (void**)pBase);
}
HRESULT CUdpSocketImpl::ReadIo(NET_ADDR addr, NET_PORT port, IBase* pBase, crtid event)
{
    HRESULT hr = S_OK;
    rc_error(pBase != NULL, E_FAIL);

    _lComPtr<IOperation> pOperation;
    hr = pBase->QueryInterface(IID_IOperation, (void**)&pOperation);
    rc_error(hr == S_OK, E_FAIL);

    return S_OK;
}
HRESULT CUdpSocketImpl::WriteIo(NET_ADDR addr, NET_PORT port, IBase* pBase, crtid event)
{
    HRESULT hr = S_OK;
    rc_error(pBase != NULL, E_FAIL);

    _lComPtr<IOperation> pOperation;
    hr = pBase->QueryInterface(IID_IOperation, (void**)&pOperation);
    rc_error(hr == S_OK, E_FAIL);

    // this->ctx.data = pBase;
    // this->ctx.event = event;

    // hr = pOperation->GetPtr(&this->ctx.wptr);
    // rc_error(this->ctx.wptr != NULL, E_FAIL)
    // rc_error(hr == S_OK, E_FAIL)

    // hr = pOperation->GetExpect(&this->ctx.wlen);
    // rc_error(hr == S_OK, E_FAIL)

    return S_OK;
}
